package PeterSheep.Utils;

import PeterSheep.Kernel;
import org.bukkit.Bukkit;

import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
import java.net.URL;
import java.sql.*;

public class PDatabase {
    private final String connectorDriverName = "com.mysql.cj.jdbc.Driver";
    private String connectionUrl, connectionUsername, connectionPassword;

    private Connection databaseConnection;
    private String databaseBindTable;

    public boolean connectDatabase(String bindingTable) throws SQLException, ClassNotFoundException {
        // @ Set driver
        try { Class.forName(this.connectorDriverName); }
        catch(ClassNotFoundException e) { e.printStackTrace(); return false; }
        // @ Connect database
        String url = String.format("//%s:%d/%s?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC",
                Kernel.pluginConfig.getString("uactSettings.databaseHost"),
                Kernel.pluginConfig.getInt("uactSettings.databasePort"),
                Kernel.pluginConfig.getString("uactSettings.databaseName"));
        Bukkit.getLogger().info(" (PeterSheep) --[#]> Connection url: " + url);
        this.databaseConnection = DriverManager.getConnection("jdbc:mysql:" + url,
                Kernel.pluginConfig.getString("uactSettings.databaseUser"),
                Kernel.pluginConfig.getString("uactSettings.databasePassword"));
        // @ Check database state
        if(!checkState()) { this.databaseConnection = null; return false; }
        if(this.databaseConnection == null)
            Bukkit.getLogger().info(" (PeterSheep) --[!]> Warning: Database open task("+ url +") a fatal error has occurred!");
        this.databaseBindTable = bindingTable;
        this.connectionUrl = url;
        this.connectionPassword = Kernel.pluginConfig.getString("uactSettings.databasePassword");
        this.connectionUsername = Kernel.pluginConfig.getString("uactSettings.databaseUser");
        return true;
    }

    public void insert2Table(String valueScript, String dataScript) throws SQLException {
        executeModifiedSQLScript(String.format("INSERT INTO %s(%s) VALUES %s",
                this.databaseBindTable, valueScript, dataScript));
    }

    public void update2Table(String filterScript, String dataScript) throws SQLException {
        executeModifiedSQLScript(String.format("UPDATE %s SET %s WHERE",
                this.databaseBindTable, dataScript, filterScript));
    }

    public void delete2Table(String filterScript) throws SQLException {
        executeModifiedSQLScript(String.format("DELETE FROM %s WHERE %s",
                this.databaseBindTable, filterScript));
    }

    void executeModifiedSQLScript(String sqlScript) throws SQLException {
        Bukkit.getLogger().info(" (PeterSheep) --[?]> Execute SQLScript(Modified): " + sqlScript);
        this.databaseConnection.createStatement().execute(sqlScript);
    }

    public Statement getConnectionStatement() throws SQLException {
        Statement statement = this.databaseConnection.createStatement();
        return statement;
    }

    public boolean checkState() throws SQLException {
        return !databaseConnection.isClosed();
    }

    Connection getConnection() {
        return databaseConnection;
    }
}
